//
//  ZYMainViewController.m
//  ZYSliderViewController
//
//  Created by zY on 16/11/10.
//  Copyright © 2016年 zY. All rights reserved.
//

#import "ZYMainViewController.h"
#import "UIViewController+ZYSliderViewController.h"
#import "ZYSliderViewController.h"
#import "CLIconVCCell.h"
#import "CLIconVCHeaderView.h"

@interface ZYMainViewController () <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataSource;
    
}
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *titleImageView;

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ZYMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubView];
}

- (void)createSubView
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.imageView.image = [UIImage imageNamed:@"home_back_img"];
    [self.view addSubview:self.imageView];
    
    [self.view addSubview:self.titleImageView];
    @weakify(self);
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(60);
        
    }];
  
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.sectionFooterHeight = 0;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"CLIconVCCell" bundle:nil] forCellReuseIdentifier:@"CLIconVCCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CLIconVCHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CLIconVCHeaderView"];
    [self.view addSubview:self.tableView];
    
    _dataSource = @[@[@"Recommend"], @[@"Quotes"], @[@"Calender"]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home_left"] style:UIBarButtonItemStylePlain target:self action:@selector(showLeftAction)];
                                                                                                                            
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home_right"] style:UIBarButtonItemStylePlain target:self action:@selector(showRightAction)];
}

- (UIImageView *)titleImageView
{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc] init];
        _titleImageView.image = [UIImage imageNamed:@"home_title"];
    }
    return _titleImageView;
}

- (void)showLeftAction
{
    [[self sliderViewController] showLeft];
}

- (void)showRightAction
{
    [[self sliderViewController] showRight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataSource[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"CLIconVCCell";
    CLIconVCCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *controller = [[UIViewController alloc] init];
    controller.view.backgroundColor = [UIColor redColor];
    [self.navigationController pushViewController:controller animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 127;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 202;
    } else {
        return 47;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *sections = _dataSource[section];
    NSString *title = sections.firstObject;
    
    if (section == 0) {
        CLIconVCHeaderView *headerView = [[NSBundle mainBundle] loadNibNamed:@"CLIconVCHeaderView" owner:nil options:nil].firstObject;
        headerView.backgroundColor = [UIColor clearColor];
        return headerView;
    } else {
        UIView *headerViw = [UIView new];
        headerViw.backgroundColor = [UIColor clearColor];
        UILabel *titleLabel = [UILabel new];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightHeavy];
        titleLabel.text = title;
        titleLabel.frame = CGRectMake(20, (47-20)/2, 200, 20);
        [headerViw addSubview:titleLabel];
        
        return headerViw;
    }
    return [UIView new];
   
}


@end
